package com.surfacing.rcp.db.util;

import java.util.GregorianCalendar;


import org.apache.log4j.Logger;

import org.eclipse.swt.graphics.Color;

/**
 * Contains convinient methods for common use
 * 
 * @author Michael
 */
public class Util {

	private static final Logger log = Logger.getLogger(Util.class);

	private static final GregorianCalendar gcal = new GregorianCalendar();

	/**
	 * Returns the color object for the given RGB values
	 * 
	 * @param r
	 *            the red value
	 * @param g
	 *            the green value
	 * @param b
	 *            the blue value
	 * @return the color object
	 */
	public final static Color getColor(int r, int g, int b) {
		return new Color(null, r, g, b);
	}

	public final static String convertGregorianToTime(long time) {
		gcal.setTimeInMillis(time);
		String changed = (gcal.get(GregorianCalendar.HOUR_OF_DAY) <= 9 ? "0"
				: "")
				+ gcal.get(GregorianCalendar.HOUR_OF_DAY)
				+ ":"
				+ ((gcal.get(GregorianCalendar.MINUTE) <= 9 ? "0" : "") + gcal
						.get(GregorianCalendar.MINUTE));

		if (log.isDebugEnabled())
			log.debug("Time: " + changed);

		return changed;
	}

	public final static String convertGregorianToDateTime(long time) {
		gcal.setTimeInMillis(time);
		StringBuilder sb = new StringBuilder();

		sb.append(String.valueOf(gcal.get(GregorianCalendar.DAY_OF_MONTH)))
				.append(".")
				.append(String.valueOf(gcal.get(GregorianCalendar.MONTH) + 1))
				.append(".")
				.append(String.valueOf(gcal.get(GregorianCalendar.YEAR)))
				.append(" ");

		String changed = (gcal.get(GregorianCalendar.HOUR_OF_DAY) <= 9 ? "0"
				: "")
				+ gcal.get(GregorianCalendar.HOUR_OF_DAY)
				+ ":"
				+ ((gcal.get(GregorianCalendar.MINUTE) <= 9 ? "0" : "") + gcal
						.get(GregorianCalendar.MINUTE));

		sb.append(changed);

		if (log.isDebugEnabled())
			log.debug("DateTime: " + sb.toString());

		return sb.toString();
	}

	public final static String convertGregorianToDate(long time) {
		gcal.setTimeInMillis(time);
		StringBuilder sb = new StringBuilder();

		sb.append(String.valueOf(gcal.get(GregorianCalendar.DAY_OF_MONTH)))
				.append(".")
				.append(String.valueOf(gcal.get(GregorianCalendar.MONTH) + 1))
				.append(".")
				.append(String.valueOf(gcal.get(GregorianCalendar.YEAR)));

		if (log.isDebugEnabled())
			log.debug("Date: " + sb.toString());

		return sb.toString();
	}
}
